package com.ttl.bc.ic.stkissue.action;

import java.util.ArrayList;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ttl.bc.ap.invoice.bo.BCAPInvoiceBO;

import com.ttl.bc.ic.stkissue.bean.BCStkIssueBean;
import com.ttl.bc.ic.stkissue.bean.ITTL_OutStock_MinDate;
import com.ttl.bc.ic.stkissue.db.BCStkIssueBO;
import com.ttl.bc.ic.stkissue.db.BCStkIssueDAO;
import com.ttl.bc.stock.request2.action.BCStkIssue2Form;

import com.ttl.common.util.CommonUtils;

public class BCStkIssueAction extends DispatchAction {
	
	@SuppressWarnings("unchecked")
	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueAction #mode :show");		
		
		BCStkIssueBO bo = new BCStkIssueBO();														
		ArrayList results = (ArrayList)bo.find();				
				
		request.getSession().setAttribute("stkIssueData", results);		
		request.getSession().setAttribute("userName", user);
		
		return mapping.findForward("success_show");
	}

	@SuppressWarnings("unchecked")
	public ActionForward showFindOLD(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueAction #mode :showFind");
		
		BCStkIssueBO bo = new BCStkIssueBO();							
		ArrayList results = (ArrayList)bo.find(request);
		
		request.getSession().setAttribute("stkIssueData", results);
		
		String monthFind = (String)request.getParameter("monthFind");
		if(monthFind==null) {
			monthFind="0";
		}
		request.setAttribute("monthFind", Integer.parseInt(monthFind));
		request.setAttribute("docNoFind", request.getParameter("docNoFind"));		
		
		return mapping.findForward("success_showFind");
	}	
	
	public ActionForward showFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueAction #mode :showFind");
		
//		new BCPurchaseOrderBO().showFind(request);
		new BCStkIssueBO().showFind(request);
		
		return mapping.findForward("success_showFind2");
	}	
	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueAction #mode :update");
		
//		BCStkIssueBO bo = new BCStkIssueBO();
//		boolean addComplete = bo.update(form, request, response);
//		
//		System.out.println("updateComplete -> "+addComplete);
						
		return mapping.findForward("success_update");
	}
	
	public ActionForward approveShow(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		BCStkIssueBO bo = new BCStkIssueBO();
		BCStkIssue2Form issueform = bo.approveShow(request, response);
		
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueAction #mode :approveShow "+
									  "#docNo:"+issueform.getDocNo());		
		
		String stkIssueDocNo = CommonUtils.getMaxStkIssueDocNo(issueform.getDocNo(), issueform.getDepartCode());//issueform.getDocNo();  
		request.setAttribute("issue2form", issueform);
		request.setAttribute("inPage", inPage);
		request.setAttribute("stkIssueDocNo", stkIssueDocNo);
//		request.getSession().setAttribute("userName", user);
		
		ITTL_OutStock_MinDate m = new BCStkIssueDAO().getMinDate();
		
		System.out.println("year ="+(m.getMinDate().getYear()+1900));
		System.out.println("month ="+(m.getMinDate().getMonth()+1));
		System.out.println("day ="+m.getMinDate().getDate());
		request.setAttribute("minDate_year", (m.getMinDate().getYear()+1900));
		request.setAttribute("minDate_month", (m.getMinDate().getMonth()+1));
		request.setAttribute("minDate_day", m.getMinDate().getDate());
		return mapping.findForward("approveShow");
	}
	
	public ActionForward approveShowCancel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
				
		
		BCStkIssueBO bo = new BCStkIssueBO();
		BCStkIssue2Form issueform = bo.approveShow(request, response);
		
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueAction #mode :approveShowCancel "+
									  "#docNo:"+issueform.getDocNo());		
		
		
		
//		String stkIssueDocNo = issueform.getDocNo();// CommonUtils.getMaxStkIssueDocNo(issueform.getDocNo());
		request.setAttribute("issue2form", issueform);
		request.setAttribute("inPage", inPage);		
		request.getSession().setAttribute("userName", user);

		BCStkIssueBO bo2 = new BCStkIssueBO();
		BCStkIssueBean issue = bo2.find(issueform.getDocNo());
		request.setAttribute("issue", issue);			
		
		return mapping.findForward("approveShowCancel");					
	}	
	
	public ActionForward approveConfirm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		BCStkIssueBO bo = new BCStkIssueBO();
		
		
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueAction #mode :approveConfirm "+
									  "#docNo:"+request.getParameter("docNo"));		
										
		request.setAttribute("inPage", inPage);		
		
		if (bo.approveConfirm(request, response)==true) {
			return mapping.findForward("success_approve");
		}else{
			return mapping.findForward("fail_approve");
		}
	}
	
	public ActionForward approveConfirmCancel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {
		
		BCStkIssueBO bo = new BCStkIssueBO();
		
		
		String inPage = (String)request.getParameter("d-49653-p");
		String user   = (String)request.getSession().getAttribute("userName");
		
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueAction #mode :approveConfirm "+
									  "#docNo:"+request.getParameter("docNo"));		
										
		request.setAttribute("inPage", inPage);		
		
		if (bo.approveConfirmCancel(request, response)==true) {
			return mapping.findForward("success_approve_cancel");
		}else{
			return mapping.findForward("fail_approve");
		}
	}
	
	public ActionForward swapShow(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueAction #mode :swapShow");		
				
		return mapping.findForward("blank_swap");
	}	
	
	public ActionForward swap(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueAction #mode :swap");		
		
		if(new BCStkIssueBO().swap(request, form)==true) {
			request.setAttribute("message", "บันทึกข้อมูลใหม่เรียบร้อย");
			return mapping.findForward("success_swap");
		}else {
			//ทำ map กับการ fail_swap
			return mapping.findForward("fail_swap");			
		}
				
		
	}	
	
	public ActionForward docNoFind(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) 
			throws Exception {

		String user 	 = (String)request.getSession().getAttribute("userName");
		System.out.println(CommonUtils.getDateTimeForStamp()+" #user:"+user+" #use :BCStkIssueAction #mode :docNoFind");		
		String docNoType = (String)request.getParameter("docNoType");
		String line = (String)request.getParameter("line");
		
		String	itemFind = "";
		if(request.getParameter("itemFind")!=null) {
			itemFind = ((String)request.getParameter("itemFind"));
			itemFind = new String(itemFind.getBytes("ISO8859_1"),"UTF-8");
			request.setAttribute("itemFind", itemFind);
		}
		request.setAttribute("docNoType", docNoType);
		request.setAttribute("line", line);		
		
		
		String hql = "";
		if(docNoType.equalsIgnoreCase("stkIssue2")) {
			hql = "FROM BCStkIssue2 WHERE docNo like '%"+itemFind+"%' ORDER BY docNo";
		}else {
			hql = "FROM BCStkRequestBean WHERE docNo like '%"+itemFind+"%' ORDER BY docNo";
		}		
		System.out.println(hql);
		new BCAPInvoiceBO().setPage(request, hql);		
		
		return mapping.findForward("success_docno_popup");				
				
	}	
}
